<template lang="">
  <div class="box">
    <el-card shadow="hover" class="form">
      <template #header>
        <h3>星克-管理系统</h3>
      </template>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model.number="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密&nbsp;码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm" style="width:150px">提交</el-button>
          <el-button @click="resetForm"  style="width:155px">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { loginRequest } from "@/api/user"
import { ElMessage } from "element-plus"
export default {
  data() {
    return {
      ruleForm: {
        pass: "123456",
        username: "admin"
      },
      //定义表单的校验规则
      rules: {
        username: [
          {
            required: true,
            message: "用户名只能是以字母开头，只能是数字字母下划线组成，至少5位",
            pattern: /^[a-zA-Z]\w{4,}$/
          }
        ],
        pass: [
          {
            required: true,
            message: "密码不能为空"
          }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      console.log(this.$refs.ruleForm) //获取表单组件
      //调用表单组件的自己方法 获取表单内部验证结果
      this.$refs.ruleForm.validate((r) => {
        console.log(r)
        if (r) {
          //通过验证
          //发起的登录请求
          loginRequest()
          loginRequest(this.ruleForm.username, this.ruleForm.pass).then((r) => {
            console.log(r.data)
            if (r.data.code == 200) {
              // 请求成功以后存入数据
              localStorage.setItem("token", r.data.data.token)
              localStorage.setItem("adminname", r.data.data.adminname)

              
              localStorage.setItem("role", r.data.data.role)
              localStorage.setItem("checkedkeys", JSON.stringify(r.data.data.checkedkeys))
              ElMessage({
                showClose: true,
                message: r.data.message,
                type: "success"
              })

              // 跳转到首页
              this.$router.push("/home")
            } else {
              ElMessage({
                showClose: true,
                message: r.data.message,
                type: "error"
              })
            }
          })
        } else {
          //没有通过验证

          ElMessage({
            showClose: true,
            message: "请检查输入框的内容",
            type: "error"
          })
        }
      })
    },
    resetForm(data) {}
  }
}
</script>
<style lang="scss">
.box {
  height: 100%;
  background-image: url("@/assets/imgs/bj.jpg");
  display: flex;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  .form {
    width: 35%;
    margin: auto;
    text-align: center;
    
  }
}
</style>
